########################################################################
#  Pyfm - personal finance data-base
#  Copyright (C) 2007,8 Ehud Ben-Reuven
#  udi@benreuven.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
########################################################################
"""
You will need to add pdftotext.exe to your PATH. It can be extracted from ZIP
file xpdf...-win32.zip downloaded from http://www.foolabs.com/xpdf/download.html
"""
from lib import dirs
import os
import logging
from scanner import *
from csv2flt import *
from obj.flat import stv,stagvalue
from lib import conf

def pre(fname,tmpname):
	rc= os.spawnl(os.P_WAIT, 'pdftotext', 'pdftotext', '-layout', fname, tmpname)
	if rc:
		logging.error('%s','converting to temp file')

t_remove_header="""*@'(?!Nominal/Number).*'@
*@'(?P<l>.*)'@"""


t_remove_tail="""*@'(?P<l>(?!Nominal/Number\s+Description\s+Market price\s+Net purchase value\s+Market value\s+P/L security / %\s+Exp. cash flow).*)'@
*@'.*'@"""

t_get_asset="""*@'(?!(([A-Z]{3})?\s{1,20}(((\-?\d{1,3}(,\d{3})*(\.\d\d)?)|0))\s{1,30})\(?\w+).*'@
@'(?P<l>.*)'@
@'(?P<l>.*)'@
@'(?P<l>.*)'@
*@'(?P<n>.*)'@"""

def flat(tmpname,fout,linetxt):
	namespace["commanumre"]=commanumre
	namespace["commaintre"]=commaintre
	namespace["eurdre"]=eurdre
	finp=open(tmpname)
	try:
		rows=scanner(t_remove_header,finp)
		txt=[]
		for r in rows:
			txt.append(r['l'])
		txt='\n'.join(txt)
		rows=scanner(t_remove_tail,txt)
		txt=[]
		for r in rows:
			txt.append(r['l'])
		txt='\n'.join(txt)
		
		acctgrp='N'
		while txt:
			secname=cur=units=acctid=secid=price=None
			rows=scanner(t_get_asset,txt)
			ctxt=[]
			ntxt=[]
			for r in rows:
				if 'l' in r: ctxt.append(r['l'])
				if 'n' in r: ntxt.append(r['n'])
			#print '\n'.join(txt)
			m=re.match(
'^((?P<CUR>[A-Z]{3})?\s{1,20}(?P<UNITS>((\-?\d{1,3}(,\d{3})*(\.\d\d)?)|0))\s{1,30})(?P<SECNAME>[^\s]+(\s[^\s]+)*)\s{4,}((?P<SECID>\w{12})(\s+(?P<YIELD>\d+\.(\d{2,}))\%?)?\s+(?P<CUR1>[A-Z]{3})\s+(?P<UNITPRICE>\d+\.\d{4}))?.*$',ctxt[0])
			if not m:
				raise Exception('bad format 1')
			mdict=m.groupdict()
			#print stv(mdict,'CUR'),stv(mdict,'CUR1'),stv(mdict,'UNITS'),stv(mdict,'SECNAME'),stv(mdict,'SECID'),stv(mdict,'PRICE')
			m=re.match('^((\s{20,30}(?P<SECNAME>[^\s]+(\s[^\s]+)*)(\s{4,}.*)?)|)$',ctxt[1])
			if not m:
				raise Exception('bad format 2')
			mdict2=m.groupdict()
			#print stv(mdict2,'SECNAME')
			txt='\n'.join(ntxt)
			cur=mdict['CUR']
			if not cur:
				cur=mdict['CUR1']
			units=mdict['UNITS']
			units=units.replace(',','')
			secname=mdict['SECNAME']
			if mdict2['SECNAME'] and not mdict2['SECNAME'].startswith('Sector: '):
				secname+=' '+mdict2['SECNAME']
			acctid=acctgrp+'i'
			if mdict['SECID']:
				secid='ISIN='+mdict['SECID']
			else:
				if secname.startswith('Current Account '):
					if not cur:
						print "Missing Currency"
					secid='CURRENT=CASH='+cur
					m=re.match('^Current Account (?P<ACCTID>\d{4}\-\d{6}\-\w+(\-\w+)?)$',secname)
					if not m:
						print "Bad Current Account", secname
					else:
						acctid=m.group('ACCTID')
				else:
					secid='CURRENT=MNYMARKET='+cur
					m=re.match('^\d+(\.\d\d)?% \-(?P<ACCTID>\d{6}\-\w+(\-\w+)?) \d{2}\.\d{2}\.\d{2}Overnight Money Fiduciary$',secname)
					if not m:
						print "Bad money market",secname
					else:
						acctid=m.group('ACCTID')
			price = mdict['UNITPRICE'] if mdict['UNITPRICE'] else "1"
			yld = None
			if mdict['YIELD']:
				price = float(price) / 100.
				yld=mdict['YIELD']
			
			print >>fout,stagvalue('SECNAME',secname),				
			print >>fout,stagvalue('CURRENCY',cur),
			print >>fout,stagvalue('UNITS',units),
			print >>fout,stagvalue('ACCTID',acctid),
			print >>fout,stagvalue('SECID',secid),
			print >>fout,stagvalue('UNITPRICE',price),
			if yld: print >>fout,stagvalue('YIELD',yld),
			print >>fout,linetxt
	except scannerror, v:
		print v
	finally:
		finp.close()

ext='*.pdf'

help="""
"""
def path(line):
	return ['directnet','soi']

